package com.jk.goodsclassify.classify.utils;

import com.jk.goodsclassify.classify.entry.Library;
import com.jk.goodsclassify.classify.entry.Location;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class DistanceUtils {

    private Library library;
    private List<String> libraryList;

    public DistanceUtils(Library library){
        this.library = library;
        libraryList = library.getDataList();
    }

    //获取所有种类平均位置
    public List<Location> getAveLocationEachCate(List<Library> libraryEachCate){
        List<Location> locations = new ArrayList<Location>();
        for(Library libraryACate:libraryEachCate){
            locations.add(getAveLocationACate(libraryACate));
        }
        batchToOne(locations);
        return locations;
    }

    //获取单个种类的平均位置
    public Location getAveLocationACate(Library libraryACate){
        Location location = new Location();
        location.setName(libraryACate.getCategory());
        Iterator<Map.Entry> iterator = libraryACate.iterator();
        while (iterator.hasNext()){
            Map.Entry<String,Integer> entry = iterator.next();
            if(library.isExist(entry.getKey())){
                Double count = (double)(entry.getValue())/(double)libraryACate.size();
                location.put(libraryList.indexOf(entry.getKey()),count);
            }
        }
        return location;
    }

    //求两点的距离
    public double getDistanceBetweenTwoPoint(Location location1,Location location2){
        double distance = 999999;
        int flag = 0; //判断是否找到
        for(Map.Entry<Integer,Double> entry1:location1.getLocation().entrySet()){
            distance += Math.pow(entry1.getKey(),2);
            for (Map.Entry<Integer,Double> entry2:location2.getLocation().entrySet()){
                distance += Math.pow(entry2.getValue(),2);
                if(entry1.getKey().equals(entry2.getKey())){
                    distance += Math.pow(entry1.getValue()-entry2.getValue(),2);
                    distance -= Math.pow(entry1.getKey(),2);
                    distance -= Math.pow(entry2.getKey(),2);
                }
            }
            //如果没匹配到
            if(flag==0){
                distance += Math.pow(entry1.getValue(),2);
            }
            flag = 0;
        }
        return distance;
    }

    //求距离最短的点
    public String geeMinDistancePoint(Location location1,List<Location> locationList){
        double minDis = 99999999;
        String targetPoint = null;
        for(Location location2 : locationList){
            double distance = getDistanceBetweenTwoPoint(location1,location2);
            if(distance<minDis){
                minDis = distance;
                targetPoint = location2.getName();
            }
        }
        return targetPoint;
    }

    //归一
    private void toOne(Location location){
        Double amount = 0.0;
        for(Map.Entry<Integer,Double> entry:location.getLocation().entrySet()){
            amount += entry.getValue();
        }
        for(Map.Entry<Integer,Double> entry:location.getLocation().entrySet()){
            location.put(entry.getKey(),entry.getValue()/amount);
        }
    }

    //批量归一
    private void batchToOne(List<Location> locations){
        for(Location location:locations){
            toOne(location);
        }
    }

}
